<template>
  <view>
    <!-- #ifndef MP-WEIXIN -->
    <c-navbar :title="pageTitle" fixed bg-color="#fff">
      <!-- #ifdef H5 -->
      <template v-if="type == 15" slot="left">
        <view />
      </template>
      <!-- #endif -->
    </c-navbar>
    <!-- #endif -->

    <view class="pageBgW pd262830 box-b">
      <view v-if="!url" class="">
        <c-rich-text v-if="richTextContent" :content="richTextContent"></c-rich-text>
        <no-data
          v-if="!richTextContent"
          classStyle="width:213rpx;height:182rpx;margin: 225rpx auto 53rpx;"
          titleColor="#808080"
          nullName="noData.png"
        ></no-data>
      </view>
      <!-- #ifdef MP-WEIXIN || H5 -->
      <web-view v-else :src="url" />
      <!-- #endif -->
    </view>
  </view>
</template>

<script>
import graceRichText from '@/common/richText.js';
import cNavbar from '@/components/c-navbar';

let wv; // 计划创建的webview
export default {
  components: {
    cNavbar,
  },
  data() {
    return {
      url: '',
      richTextContent: '',
      pageTitle: '',
      type: '',
    };
  },
  destroyed() {
    const vm = this;
    vm.clearStorageSync();
  },
  onLoad(e) {
    if (e) {
      if (e.url) this.url = decodeURIComponent(e.url);
      if (e.type) {
        this.type = e.type;
        if (e.type == 1) {
          this.api_ui_getguidepic(e.type);
        }
        if (e.type == 2) {
          this.api_ui_getfulltxt(e.type);
        }
      }
      if (e.richText) {
        // #ifdef H5
        this.richTextContent = this.filterSpecialSymbol(uni.getStorageSync('swiperContent'));
        // #endif

        // #ifndef H5
        this.richTextContent = graceRichText.format(uni.getStorageSync('swiperContent'));
        // #endif
      }
      if (e.title) {
        this.pageTitle = e.title;
        uni.setNavigationBarTitle({
          title: e.title,
        });
      }
    }

    // #ifdef APP-PLUS
    if (this.url) {
      wv = plus.webview.create('', 'custom-webview', {
        plusrequire: 'none', // 禁止远程网页使用plus的API，有些使用mui制作的网页可能会监听plus.key，造成关闭页面混乱，可以通过这种方式禁止
        'uni-app': 'none', // 不加载uni-app渲染层框架，避免样式冲突
        top: uni.getSystemInfoSync().statusBarHeight + 44, // 放置在titleNView下方。如果还想在webview上方加个地址栏的什么的，可以继续降低TOP值
      });
      wv.loadURL(this.url);
      const currentWebview = this.$mp.page.$getAppWebview(); // 获取当前页面的webview对象
      currentWebview.append(wv); // 一定要append到当前的页面里！！！才能跟随当前页面一起做动画，一起关闭
      // setTimeout(function() {
      // 	console.log(wv.getStyle())
      // }, 1000);//如果是首页的onload调用时需要延时一下，二级页面无需延时，可直接获取
    }
    // #endif
  },
  methods: {
    clearStorageSync() {
      let swiperContent = uni.getStorageSync('swiperContent');
      if (swiperContent) {
        uni.removeStorageSync('swiperContent');
      }
    },
    api_ui_getguidepic(type) {
      const vm = this;
      uni.showLoading({
        title: 'loading...',
      });
      vm.apiUrl
        .api_ui_getguidepic({
          data: {
            type,
          },
        })
        .then(async (res) => {
          uni.hideLoading();
          const { code, data, msg: message } = res.data;
          if (code == 0) {
            let repairnotice = await vm.filterSpecialSymbol(data.repairnotice);
            vm.richTextContent = repairnotice;
          } else {
            vm.showMessage({
              title: message,
              icon: 'error',
              duration: 1500,
            });
          }
        });
    },
    api_ui_getfulltxt(type) {
      const vm = this;
      uni.showLoading({
        title: 'loading...',
      });
      let keyname = '';
      let keyObj = {
        1: 'repairnotice',
        2: 'repairconnect',
      };
      for (let path in keyObj) {
        if (path == type) {
          keyname = keyObj[path];
          break;
        }
      }
      vm.apiUrl
        .api_ui_getfulltxt({
          data: {
            keyname: keyname,
          },
        })
        .then(async (res) => {
          uni.hideLoading();
          const { code, data, msg: message } = res.data;
          if (code == 0) {
            let content = await vm.filterSpecialSymbol(data.content);
            vm.richTextContent = content;
          } else {
            vm.showMessage({
              title: message,
              icon: 'error',
              duration: 1500,
            });
          }
        });
    },
  },
};
</script>

<style lang="less" scoped></style>
